package com.loma.common.base

import android.text.TextUtils
import android.util.Log
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.loma.common.R
import com.loma.common.utils.ext.setPath
import com.loma.common.utils.util.dpToPx
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import java.text.SimpleDateFormat
import java.util.*
import kotlin.jvm.Throws

object BindingAdapterUtils {
    private val TAG = BindingAdapterUtils::class.qualifiedName

    @JvmStatic
    @BindingAdapter("url")
    fun setImgURL(view: ImageView, url: String?) {
        url?.let {
//            Glide.with(view.context).load(it)
//
//                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
//                .skipMemoryCache(false)
//                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                //设置图片加载的优先级
//                .priority(Priority.HIGH)
//                .into(view)
            view.setPath(it)

        }
        if (url!=null){
            view.setPath(url)
        }else{

            view.setImageResource(R.drawable.placeholder)
        }



    }


    @JvmStatic
    @BindingAdapter("icon")
    fun setImgURL(view: ImageView, resId: Int?) {

        resId?.let {

            view.setImageResource(it)
        }


    }



    /**
     * 展示网络图片
     *
     * @param imgView 图片控件对象
     * @param url     图片加载地址
     * @param radius  图片圆角范围，单位为dip
     */
    @JvmStatic
    @BindingAdapter("url", "radius")
    fun setImgURL(view: ImageView, url: String?, radius: Float) {

        url?.let {
            Glide.with(view.context).load(it)

                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                .skipMemoryCache(false)
                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                //设置图片加载的优先级
                .priority(Priority.HIGH)
                .transform(RoundedCornersTransformation(view.context.dpToPx(radius).toInt(), 0))
                .into(view)
        }

    }


//    /**
//     * 展示网络图片
//     *
//     * @param imgView 图片控件对象
//     * @param url     图片加载地址
//     * @param radius  图片圆角范围，单位为dip
//     */
//    @JvmStatic
//    @BindingAdapter("url", "radius")
//    fun setImgRadius(view: ImageView, url: String?, radius: Float) {
//
//        url?.let {
//            Glide.with(view.context).load(it)
//
//                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
//                .skipMemoryCache(false)
//                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                //设置图片加载的优先级
//                .priority(Priority.HIGH)
//                .transform(RoundedCornersTransformation(view.context.dpToPx(radius).toInt(), 0))
//                .into(view)
//        }
//
//    }


    @JvmStatic
    @BindingAdapter("text_int")
    fun setIntValue(view: EditText, v: Int?) {
        v?.let {
            view.setText(it.toString())
        }

    }

    @JvmStatic
    @BindingAdapter("text_int")
    fun setTextViewIntValue(view: TextView, v: Int?) {
        v?.let {
            view.setText(it.toString())
        }

    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_int", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getIntValue(view: EditText): Int? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toInt()
        } catch (e: Exception) {
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_float")
    fun setFloatValue(view: EditText, v: Float?) {
        v?.let {
            view.setText(it.toString())
        }

    }

    @JvmStatic
    @BindingAdapter("text_float")
    fun setTextViewFloatValue(view: TextView, v: Float?) {
        v?.let {
            view.setText(it.toString())
        }

    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_float", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getFloatValue(view: EditText): Float? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toFloat()
        } catch (e: Exception) {
            Log.e(BindingAdapterUtils.TAG, "String转Float数据转换出错" + e.toString())
            throw e
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_long")
    fun setLongValue(view: EditText, v: Long?) {
        v?.let {
            view.setText(it.toString())
        }
    }

    @JvmStatic
    @BindingAdapter("text_long")
    fun setTextViewLongValue(view: TextView, v: Long?) {
        v?.let {
            view.setText(it.toString())
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_long", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getLongValue(view: EditText): Long? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toLong()
        } catch (e: Exception) {
            throw e
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_double")
    fun setDoubleValue(view: EditText, v: Double?) {
        v?.let {
            view.setText(it.toString())
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_double", event = "android:textAttrChanged")
    @Throws(
        NumberFormatException::class
    )
    fun getDoubleValue(view: EditText): Double? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toDouble()
        } catch (e: Exception) {
            throw  e
        }
    }

    @JvmStatic
    @BindingAdapter("text_boolean")
    fun setBooleanValue(view: EditText, v: Boolean?) {
        v?.let {
            view.setText(if (it) "是" else "否")
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_boolean", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getBooleanValue(view: EditText): Boolean? {
        val string = view.text.toString()

        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            return string == "是"

        } catch (e: Exception) {
            throw e
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_date")
    fun setDateValue(view: EditText, v: Date?) {
        v?.let {
            view.setText(SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(v))
        }
    }

    @JvmStatic
    @BindingAdapter("text_date")
    fun setTextViewDateValue(view: TextView, v: Date?) {
        v?.let {
            view.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(v)
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_date", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getDateValue(view: EditText): Date? {
        val string = view.text.toString()

        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            return SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string)

        } catch (e: Exception) {
            throw e
            null
        }
    }

    //    @BindingConversion
//    fun convertDate(date: Date?): String {
//        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
//        return sdf.format(date)
//    }
//
//    @BindingConversion
//    fun convertString(i: Int): String {
//        return i.toString()
//    }
    @JvmStatic
    @InverseBindingAdapter(attribute = "checked", event = "android:checkedAttrChanged")
    fun getchecked(view: CheckBox): Boolean {
        return view.isChecked
    }



    @JvmStatic
    @BindingAdapter("text_string")
    fun setStringValue(view: EditText, v: String?) {
        v?.let {
            view.setText(it)
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_string", event = "android:textAttrChanged")
    fun getStringValue(view: EditText): String? {
        val string = view.text.toString()

        return if (TextUtils.isEmpty(string)) "" else string
    }

}